import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'image_loading_state_view.dart';

class CustomImage extends ExtendedImage {
  CustomImage.network(
    url, {
    Key? key,
    double? width,
    double? height,
    Color? color,
    BlendMode? colorBlendMode,
    BoxFit fit = BoxFit.cover,
    BoxShape shape = BoxShape.rectangle,
    BoxBorder? border,
    BorderRadius borderRadius = const BorderRadius.all(Radius.zero),
    LoadStateChanged? loadStateChanged,
    Color loadingViewBackgroundColor = const Color(0xffefefef),
    bool isResizeImage = false,
    double scale = 1.5,
    bool enableMemoryCache = true,
    bool clearMemoryCacheIfFailed = true,
    bool clearMemoryCacheWhenDispose = false,
    int retries = 3,
    Function? getImageSize,
    bool isShowLoadingState = true,
  }) : super(
          key: key,
          image: isResizeImage
              ? ResizeImage.resizeIfNeeded(
                  width == null ? null : (width * scale).ceil(),
                  height == null ? null : (height * scale).ceil(),
                  ExtendedNetworkImageProvider(
                    url ?? '',
                    cache: true,
                    retries: retries,
                  ),
                )
              : ExtendedNetworkImageProvider(
                  url ?? '',
                  cache: true,
                  retries: retries,
                ),
          width: width,
          height: height,
          color: color,
          colorBlendMode: colorBlendMode,
          fit: fit,
          enableMemoryCache: enableMemoryCache,
          clearMemoryCacheIfFailed: clearMemoryCacheIfFailed,
          clearMemoryCacheWhenDispose: clearMemoryCacheIfFailed,
          shape: shape,
          border: border,
          borderRadius: borderRadius,
          loadStateChanged: loadStateChanged ??
              (ExtendedImageState state) {
                if (isShowLoadingState) {
                  switch (state.extendedImageLoadState) {
                    case LoadState.loading:
                      return ImageLoadingStateView(
                        width ?? double.infinity.w,
                        height ?? 200.w,
                        borderRadius,
                      );
                    case LoadState.failed:
                      return ImageLoadingStateView(
                        width ?? double.infinity.w,
                        height ?? 200.w,
                        borderRadius,
                      );
                    case LoadState.completed:
                      if (getImageSize != null) {
                        Size size = Size(
                            state.extendedImageInfo!.image.width.toDouble(),
                            state.extendedImageInfo!.image.height.toDouble());
                        getImageSize(size);
                      }
                      return null;
                    default:
                      return null;
                  }
                } else {
                  switch (state.extendedImageLoadState) {
                    case LoadState.loading:
                    case LoadState.failed:
                    case LoadState.completed:
                      if (getImageSize != null) {
                        Size size = Size(
                            state.extendedImageInfo!.image.width.toDouble(),
                            state.extendedImageInfo!.image.height.toDouble());
                        getImageSize(size);
                      }
                      return null;
                    default:
                      return null;
                  }
                }
              },
        );

  //头像
  CustomImage.loadAvatarUrl(
    String url, {
    Key? key,
    double? width,
    Color? color,
    BlendMode? colorBlendMode,
    BoxBorder? border,
    BoxShape shape = BoxShape.circle,
    BorderRadius borderRadius = const BorderRadius.all(Radius.circular(1000)),
  }) : super(
          key: key,
          image: ExtendedNetworkImageProvider(url, cache: true),
          width: width,
          height: width,
          color: color,
          colorBlendMode: colorBlendMode,
          shape: shape,
          borderRadius: borderRadius,
          border: border,
          fit: BoxFit.cover,
          loadStateChanged: (state) {
            switch (state.extendedImageLoadState) {
              case LoadState.failed:
                return ImageLoadingStateView(
                  width!,
                  width,
                  borderRadius,
                );
              case LoadState.loading:
                return ImageLoadingStateView(
                  width!,
                  width,
                  borderRadius,
                );
              default:
                return null;
            }
          },
        );
}
